package cn.com.generaldata.os.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.entity.Record;
import org.nutz.dao.impl.sql.SqlTemplate;
import org.nutz.dao.sql.Criteria;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.generaldata.jsme_indicator_system.util.Constants.IndicatorAccountingReferenceType;
import cn.com.generaldata.os.service.indicator_constant.ProvinceIndicatorConstant;
import cn.com.generaldata.os.service.indicator_evaluation.development_index.TwoLevelEevelopmentIndexCalculateService;
import cn.com.generaldata.os.vo.IndicatorAccountVO;

/**
 * 评估指标数据源说明类
 */
@Service
public class CalculateExplainService {

	private static Logger logger = LoggerFactory.getLogger(CalculateExplainService.class);
	
	@Autowired
	private Dao dao;
	
	@Autowired
	private Dao indicatorSystemDao;
	
	@Autowired
	private SqlTemplate sqlTemplate;

	@Autowired
	private TwoLevelEevelopmentIndexCalculateService twoLevelEevelopmentIndexCalculateService;
	
	/**
	 * 海洋发展指数源数据
	 * */
	public List<Map> getDevelopmentIndexSourceData(int id, String area_id){

		twoLevelEevelopmentIndexCalculateService.setIndicatorSystemDao(indicatorSystemDao);
		twoLevelEevelopmentIndexCalculateService.setDao(dao);
		
		List<Map> listData = new ArrayList<Map>();
		
		List<Record> list = getDevelopmentIndexSubData(id);
		
		Map<String, String> data = null;
		for (Record re : list){
			data = new HashMap<String, String>();
			data.put("id", re.getString("id"));
			data.put("di_name", re.getString("di_name"));
			data.put("di_weight", re.getString("di_weight"));
			data.put("di_type", re.getString("di_type"));
			data.put("di_parent", re.getString("di_parent"));
			data.put("indicator_type", re.getString("indicator_type"));
			
			List<Record> subList =  getDevelopmentIndexSubData(re.getInt("id"));
			if (subList!=null&&subList.size()>0){
				data.put("state", "closed");
			}else{
				data.put("state", "open");
			}
			
			List<Record> yearListData = getIndicatorValue(re.getInt("indicator_type"), re.getString("indicator_name"), area_id);
			
			for (Record re2 : yearListData){
				data.put("year"+re2.getString("year"), re2.getString("indicator_value"));
				
				if (IndicatorAccountingReferenceType.ACCOUNTING.getValue()==re.getInt("indicator_type")){
					data.put("accId"+re2.getString("year"), re2.getString("id"));
				}
				
			}
			
			
			listData.add(data);
		}
		
		return listData;
	}
	
	public List<Record> getDevelopmentIndexYearList(String area_id){
		String sql = " select distinct d.year  from t_indicator_di_info t, t_indicator_input i, tbl_indicator_data d ";
			sql += " where d.indicator_id = i.id  and t.di_name = i.name  and t.di_type is not null ";
			sql += " and d.year >= " + ProvinceIndicatorConstant.DEVELOPMENT_INDEX_BASE_PERIOD + " and d.area_id = '" + area_id + "' order by d.year ";
		
		return sqlTemplate.queryRecords(sql, null, null);
	}
	
	/**
	 * 海洋发展指数子节点数据
	 * */
	private List<Record> getDevelopmentIndexSubData(int id){
		Criteria cri = Cnd.cri();
		cri.where().andEquals("di_parent", id);		
		List<Record> list = dao.query("t_indicator_di_info", cri);
		return list;
	}
	
	/**
	 * 海洋发展指数节点数据
	 * */
	public Record getDevelopmentIndexById(int id){
		Criteria cri = Cnd.cri();
		cri.where().andEquals("id", id);		
		Record re = dao.fetch("t_indicator_di_info", cri);
		return re;
	}
	
	/**
	 * 获取录入指标数据
	 * 
	 * @param name 指标名称
	 * @param area_id 所属地区
	 * @return
	 */

	public List<Record> getIndicatorInputDataId(String name, String area_id) {
		String sql = " select d.year, d.indicator_value from  t_indicator_input i, tbl_indicator_data d ";
			sql += " where d.indicator_id = i.id  and i.name = '" + name + "' ";
			sql += " and d.area_id = '" + area_id + "' order by d.year";
		
		return sqlTemplate.queryRecords(sql, null, null);

	}
	
	/**
	 * 获取发展指数基础指标相关值（核算指标、录入指标）
	 * */
	public List<Record> getIndicatorValue(int indicatorType, String indicatorName, String area_id){
		List<Record> list = null;
		if (IndicatorAccountingReferenceType.ACCOUNTING.getValue()==indicatorType){
			list = twoLevelEevelopmentIndexCalculateService.getIndicatorAccount(indicatorName, null, 1, 5, 7);	//获取省级核算相关指标值
			
			for (Record rc : list){
				rc.set("INDICATOR_VALUE", rc.getString("EDIT_VALUE"));	//取核算修正值
			}
		}else{
			list = getIndicatorInputDataId(indicatorName, area_id);
		}
		return list;
	}
}
